package com.yunfei.schedule;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yunfei.bo.FeiJingZhiLeiProdBo;
import com.yunfei.bo.FundFeneSureBook;
import com.yunfei.bo.TCustBuyinBo;
import com.yunfei.bo.TSpciProdBo;
import com.yunfei.contants.FundFeneSureBookContants;
import com.yunfei.entity.LogContractGetEntity;
import com.yunfei.entity.TCustBuyinEntity;
import com.yunfei.entity.TSpciProdEntity;
import com.yunfei.entity.TblobstorageEntity;
import com.yunfei.mapper.test1.TblobtSorageMapper;
import com.yunfei.mapper.test2.TCustBuyinMapper;
import com.yunfei.mapper.test2.TLogContractGetMapper;
import com.yunfei.mapper.test2.TSpciProdMapper;
import com.yunfei.service.SchedualServiceHi;
import com.yunfei.utils.ComponentFactory;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 下线定时任务，用service_electronic_signature服务替代
*/
/*@Component*/
public class FeiJingZhiLeiProdScheduledTask {
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(FeiJingZhiLeiProdScheduledTask.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");

    /**
     * 每隔5秒执行, 单位：ms。
     */
  /*  @Scheduled(fixedRate = 5000)
    public void testFixRate() {
        System.out.println("我每隔5秒冒泡一次：" + dateFormat.format(new Date()));
        Logger.info("打印当前时间: {}.", dateFormat.format(new Date()));
    }*/

   //@Scheduled(cron = "0 0 1 * * ?")   //每天凌晨1点执行
   @Scheduled(cron = "0 0/1 * * * ?")    //每1小时
   /*@Scheduled(fixedRate = 50000000)*/
    public void testMyBatis() {
        System.out.println("我是每天凌晨1点开始执行");
        try {

            TCustBuyinMapper tCustBuyinMapper = ComponentFactory.getBean(TCustBuyinMapper.class);
            TblobtSorageMapper tblobtSorageMapper = ComponentFactory.getBean(TblobtSorageMapper.class);
            TLogContractGetMapper tLogContractGetMapper = ComponentFactory.getBean(TLogContractGetMapper.class);
            TSpciProdMapper tSpciProdMapper = ComponentFactory.getBean(TSpciProdMapper.class);
            //获取要生成的数据
            List<FeiJingZhiLeiProdBo> list = tCustBuyinMapper.findAllFeiJingZhiLeiProd();
            List<FundFeneSureBook> fundFeneSureBookList=null;
            System.out.println("保理产品，需要生成认购书的总数===================================="+list.size());
            Gson gson=new Gson();
            if(list.size()>0){
                fundFeneSureBookList=new ArrayList<>();
                for(int i=0;i<list.size();i++){
                    FeiJingZhiLeiProdBo feiJingZhiLeiProdBo=list.get(i);
                  //  System.out.println("认购书信息==========="+gson.toJson(feiJingZhiLeiProdBo));
                    String lMandatorId=feiJingZhiLeiProdBo.getlMandatorId();//管理人ID
                   // System.out.println("根据产品编号:"+lMandatorId+"查询对应的模板名称start.........................");
                    //根据上述sql管理人ID 查找对应文件
                    TSpciProdBo tSpciProdBo=new TSpciProdBo();
                    tSpciProdBo.setProdId(lMandatorId);
                    TSpciProdEntity tSpciProdEntity=tSpciProdMapper.findByBo(tSpciProdBo);
                  //  System.out.println("查询到的模板为====================="+tSpciProdEntity);
                    FundFeneSureBook fundFeneSureBook=FeiJingZhiLeiProdBo.getFeneBook(feiJingZhiLeiProdBo);
                    fundFeneSureBook.setSourcePath(tSpciProdEntity.getPurLett());
                    //根据产品编号查询生成的认购书或者追加书模板的名称，配置在T_SPCI_PROD表里面，此外需要将模板放在项目service_electronic_signatu的reresources下面
                    //查询crm附件序号
                    String cSubKeyId=feiJingZhiLeiProdBo.getlBenserialNo();
                    fundFeneSureBook.setPkId(cSubKeyId);
                    String lStorageid=tblobtSorageMapper.getTblobStorageSeq()+"";
                    fundFeneSureBook.setlStorageid(lStorageid);
                    fundFeneSureBook.setCyType(FundFeneSureBookContants.CON_TYP_STATUS_02);
                    fundFeneSureBookList.add(fundFeneSureBook);
                }
                SchedualServiceHi schedualServiceHi = ComponentFactory.getBean(SchedualServiceHi.class);
                String jsonData= gson.toJson(fundFeneSureBookList);
                // String jsonData= JSONUtils.toJSONString(fundFeneSureBook);
                System.out.println("开始去调用生成认购书的接口前。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
                String resultJson=schedualServiceHi.createSignFeiJingZhiProd(jsonData);
                // System.out.println("调用生成认购书服务返回结果=============="+resultJson);
                //处理返回结果
                List <FundFeneSureBook> resultBackList=gson.fromJson(resultJson,new TypeToken<List<FundFeneSureBook>>(){}.getType());
                //定时任务可以做耗时操作，包括做生成数据库报表、文件IO等等需要定时执行的逻辑
                List<TblobstorageEntity> tblobstorageEntityList=new ArrayList<>();
                    System.out.println("一共多少条数据======================"+resultBackList.size());
                    for(int i=0;i<resultBackList.size();i++){
                        FundFeneSureBook fundFeneSureBook=resultBackList.get(i);
                        String storId=fundFeneSureBook.getlStorageid();//上述sql的
                        //生成附件表到Crmku
                        TblobstorageEntity tblobstorageBo=new TblobstorageEntity();
                        tblobstorageBo.setLStorageid(storId+"");
                        String cSubkeyId=fundFeneSureBook.getPkId();
                        tblobstorageBo.setCSubkeyid(cSubkeyId);
                        String fileName=fundFeneSureBook.getFileName(); //生成的文件名
                        String fileSize=fundFeneSureBook.getFileSize();//文件大小
                        tblobstorageBo.setCFilename(fileName);
                        tblobstorageBo.setLFilesize(fileSize);
                        tblobstorageEntityList.add(tblobstorageBo);
                        tblobtSorageMapper.addFeiJingZhiLeiProd(tblobstorageBo);
                        System.out.println("定时任务执行job"+(i+1)+":生成crm附件表ok" );
                    }
                    //System.out.println("开始批量插入日志信息。。。。。。。。。。。。。。。。。。。。");
                    // tblobtSorageMapper.batchInsertFeiJingZhiLeiProd(tblobstorageEntityList);
                    System.out.println(list.size()+"条任务处理完成");

            }else{
                System.out.println("我什么事都不用做，等待下次再说吧");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }
    }
}

